package me.stone.stanimeclient.core.data.db

import androidx.paging.PagingSource
import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query

@Entity(tableName = "search_history_fts")
data class SearchHistory(
    @PrimaryKey
    @ColumnInfo(name = "content")
    val content: String,
)

@Dao
interface SearchHistoryDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(history: SearchHistory)

    @Delete
    suspend fun delete(history: SearchHistory)

    @Query("DELETE FROM search_history_fts")
    suspend fun clearData()

    @Query("SELECT COUNT(content) FROM search_history_fts")
    suspend fun listCount(): Int

    @Query("SELECT content FROM search_history_fts")
    fun listAll(): PagingSource<Int, String>

    @Query("SELECT COUNT(content) FROM search_history_fts WHERE content LIKE '%' || :query || '%'")
    suspend fun matchCount(query: String): Int

    @Query("SELECT content FROM search_history_fts WHERE content LIKE '%' || :query || '%'")
    fun match(query: String): PagingSource<Int, String>

}
